﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using System;
using XCI.Extensions;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmRoleDetails : BaseForm
    {
        public FrmRoleDetails(string id)
        {
            InitializeComponent();
            tabPane.RecordId = id;
        }

        public FrmRoleDetails(GridControlPlus gridControl)
        {
            InitializeComponent();
            tabPane.Grid = gridControl;
        }

        private async void FrmAppDetails_Load(object sender, EventArgs e)
        {
            await tabPane.BindingModelAsync();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogCancel();
        }

        private void labDeptName_Click(object sender, EventArgs e)
        {
            var model = bindingSource.DataSource as SysRole;
            if (model == null) return;
            new FrmDeptDetails(model.DeptId).ShowDialogOk();
        }

        private void TabPane_PageBinding(object sender, DetailsFormPaneBindingEventArgs e)
        {
            e.Task = async () =>
            {
                switch (e.Current.TabPageName)
                {
                    case nameof(tabBasic):
                        bindingSource.DataSource = string.IsNullOrEmpty(e.Current.Id) ? new SysRole() : await SysRoleService.Instance.SelectByIdAsync(e.Current.Id);
                        break;
                    case nameof(tabRoleMember):
                        await userListRoleControl1.LoadAsync(e.Current.Id);
                        break;
                    case nameof(tabModulePermission):
                        await treeOwnerModule.LoadByRoleIdAsync(e.Current.Id);
                        break;
                    case nameof(tabDeptPermission):
                        await deptOwnerTreeList1.LoadByRoleAsync(e.Current.Id);
                        break;
                    case nameof(tabHistoryLog):
                        await historyLogGrid1.LoadAsync(nameof(SysRole), e.Current.Id);
                        break;
                }
            };
        }
    }
}
